<template>
  <!-- visible是否显示 没有.sync就是单向绑定 -->
  <el-dialog title="新增部门" :visible="showDialog" @close="close">
    <!-- 绘制表单 -->
    <el-form ref="form" :model="formData" :rules="formRules" label-width="140px" :inline="false" size="normal">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="formData.name" style="width: 80%" size="mini" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="formData.code" style="width: 80%" size="mini" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="managerId">
        <!-- 下拉组件 -->
        <el-select v-model="formData.managerId" style="width: 80%" size="mini">
          <el-option v-for="item in managerList" :key="item.id" :label="item.username" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="formData.introduce" type="textarea" style="width: 80%" size="mini" />
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" @click="btnOk">提交</el-button>
        <el-button size="small" @click="close">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script>
import { getDepartmentManagerApi, getDepartmentListApi, addDepartmentApi } from '@/api/department'
export default {
  name: 'AddDept',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    parentId: {
      type: [Number, String],
      default: ''
    }
  },
  data() {
    return {
      formData: {
        'code': '',
        'introduce': '',
        'managerId': '',
        'name': ''
      },
      formRules: {
        code: [
          { required: true, message: '请输入', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在2-10', trigger: 'blur' },
          { trigger: 'blur', validator: async(rule, value, cb) => {
            // 1. 找到所有部门数据(再获取一次)
            const res = await getDepartmentListApi()
            // 2. 是否已经存在
            res.filter(item => item.code === value).length ? cb(new Error('部门编码已存在')) : cb()
          } }
        ],
        introduce: [{ required: true, message: '请输入', trigger: 'blur' },
          { min: 1, max: 100, message: '长度在1-100', trigger: 'blur' }],
        name: [
          { required: true, message: '请输入', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在2-10', trigger: 'blur' },
          { trigger: 'blur', validator: async(rule, value, cb) => {
            // 1. 找到所有部门数据(再获取一次)
            const res = await getDepartmentListApi()
            // 2. 是否已经存在
            res.filter(item => item.name === value).length ? cb(new Error('部门名称已存在')) : cb()
          } }
        ],
        managerId: [{ required: true, message: '请输入', trigger: 'blur' }]
      },
      managerList: []
    }
  },
  created() {
    this.getManagerList()
  },
  methods: {
    close() {
      this.$refs.form.resetFields() // 重置表单
      this.$refs.form.clearValidate() // 移除校验
      this.$emit('update:showDialog', false)
    },
    // 获取部门负责人数据
    async getManagerList() {
      const res = await getDepartmentManagerApi()
      this.managerList = res
    },
    // 提交事件
    async btnOk() {
      // 1. 表单校验
      await this.$refs.form.validate()
      // 2. 数据处理
      const formData = { ...this.formData, pid: this.parentId }
      // 3. 发送请求
      await addDepartmentApi(formData)
      // 4. 提示成功
      this.$message.success('操作成功')
      // 5. 关闭
      this.close()
      // 6. 通知列表更新
      this.$emit('updateList')
    }
  }
}
</script>
